package ex05.pyrmont.core;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Contained;
import org.apache.catalina.Container;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.comet.CometEvent;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

public class SimpleContextValve implements Valve, Contained {

	@Override
	public Container getContainer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setContainer(Container arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void backgroundProcess() {
		// TODO Auto-generated method stub

	}

	@Override
	public void event(Request arg0, Response arg1, CometEvent arg2)
			throws IOException, ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public String getInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Valve getNext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void invoke(Request request, Response response) throws IOException,
			ServletException {
		if (!(request.getRequest() instanceof HttpServletRequest) || 
				!(response.getResponse() instanceof HttpServletResponse)) {
			return;
		}
		
		// Disallow any direct access to resources under WEB-INF or META-INF
		HttpServletRequest hreq = request.getRequest();
		String contextPath = hreq.getContextPath();
		String requestURI = request.getDecodedRequestURI();
		String relativeURI = requestURI.substring(contextPath.length()).toUpperCase();
		
		// Select the Wrapper to be used for this Request
		Wrapper wrapper = request.getWrapper();
		if (wrapper == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}

	@Override
	public boolean isAsyncSupported() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setNext(Valve arg0) {
		// TODO Auto-generated method stub

	}

}
